# coding: utf-8
# @Author : lryself
# @Date : 2020/12/10 21:02
# @Software: PyCharm
import os

from flask import request, jsonify, make_response, send_from_directory

from ..utils.response_code import RET

# http://127.0.0.1:5000/api/download?output_path=./app/static&output_name=statistics_csv_files.zip


def download_file():
    output_path = request.args.get("output_path")
    output_name = request.args.get("output_name")
    try:
        # 发送文件
        response = make_response(send_from_directory(os.path.abspath(output_path), output_name, as_attachment=True))
        response.headers["Content-Disposition"] = "attachment; filename={}".format(
            output_name.encode().decode('latin-1'))
        return response
    except Exception as e:
        return jsonify(code=RET.IOERR, message="文件不存在")
